package com.web.controller.system;

import com.domain.system.Module;
import com.domain.system.Role;
import com.domain.system.User;
import com.github.pagehelper.PageInfo;
import com.service.system.ModuleService;
import com.service.system.RoleService;
import com.service.system.UserService;
import com.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        PageInfo pageInfo =  roleService.findAll(companyId, page, size);
        request.setAttribute("page", pageInfo);
        return "system/role/role-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        List<Role> deptList = roleService.findAll(companyId);
        request.setAttribute("deptList", deptList);
        return "system/role/role-add";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Role role = roleService.findById(id);
        List<Role> deptList = roleService.findAll(companyId);
        request.setAttribute("deptList", deptList);
        request.setAttribute("role", role);
        return "system/role/role-update";
    }

    @RequestMapping("/edit")
    public String toAdd(Role role){
        role.setCompanyId(companyId);
        role.setCompanyName(companyName);
        if (StringUtils.isBlank(role.getId())) {
            Integer result = roleService.save(role);
        }else{
            Integer result = roleService.update(role);
        }

        return "redirect:/system/role/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id){
        Integer result = roleService.delete(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping("/roleModule")
    public String roleModule(String roleId){
        Role role = roleService.findById(roleId);
        //传递到前端,展示name
        request.setAttribute("role", role);
        return "system/role/role-module";
    }


    /**
     * [{id:11,  pId:1,  name:"随意勾选 1-1"},
     * {id:111, pId:11, name:"随意勾选 1-1-1", checked:true}]
     */
    @RequestMapping("/initModuleData")
    //初始化前端,要传递的数据
    public @ResponseBody List<Map> initModuleData(String roleId){
        List<Map> list = new ArrayList<>();
        //1.查询所有的权限模块
        List<Module> moduleList = moduleService.findAll();
        //2.查询该角色拥有的权限模块
        List<Module> roleList = moduleService.findByRoleId(roleId);
        //3.遍历所有的权限模块,并添加
        for (Module module : moduleList) {
            Map map = new HashMap<>();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());//注意pId的大写
            map.put("name",module.getName());

            //如果角色有该权限,添加 checked:true
            for (Module module1 : roleList) {
                if (module1.getId().equals(module.getId())) {
                    map.put("checked", true);
                }
            }
            //向list添加该模块数据
            list.add(map);
        }
        //响应到前端
        return list;
    }

    @RequestMapping("/updateRoleModule")
    public String updateRoleModule(String roleId, String moduleIds){
        moduleService.updateRoleModule(roleId, moduleIds);
        return "redirect:/system/role/list.do";
    }

}
